﻿#region DaReportAudit
/*----------------------------------------------------------------
// 文件名：DaReportAudit.cs
// 功能描述：表dbo.Report_Audit的数据访问层
//
// 
// 创建时间：2009-11-20 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,IReportAudit ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaReportAudit : DaBase<MoReportAudit>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " RA_ID ,REPORT_ID ,AUDITOR_ID ,FLOW_CODE ,BEGIN_STATUS ,END_STATUS ,AUDIT_TIME ,CREATE_TIME ,AUDIT_INFO   ";

        internal const string SQL_INSERT = "INSERT INTO REPORT_AUDIT (" + TABLE_COLUMNS + ") VALUES (@RaId,@ReportId,@AuditorId,@FlowCode,@BeginStatus,@EndStatus,@AuditTime,@CreateTime,@AuditInfo)";
        internal const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM REPORT_AUDIT  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE RA_ID=@RaId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM REPORT_AUDIT  WHERE RA_ID=@RaId  ";
        internal const string SQL_UPDATE = "UPDATE REPORT_AUDIT SET REPORT_ID=@ReportId, AUDITOR_ID=@AuditorId, FLOW_CODE=@FlowCode, BEGIN_STATUS=@BeginStatus, END_STATUS=@EndStatus, AUDIT_TIME=@AuditTime, CREATE_TIME=@CreateTime, AUDIT_INFO=@AuditInfo WHERE RA_ID=@RaId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM REPORT_AUDIT ";
        internal const string SQL_DELETE = "DELETE FROM REPORT_AUDIT WHERE RA_ID=@RaId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM Report_Audit ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaReportAudit()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaReportAudit(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaReportAudit(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoReportAudit ConstructT()
        {
            return new MoReportAudit();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "RaId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "ReportId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "AuditorId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "FlowCode", DbType.AnsiString);
            db.AddInParameter(dbCommand, "BeginStatus", DbType.Int32);
            db.AddInParameter(dbCommand, "EndStatus", DbType.Int32);
            db.AddInParameter(dbCommand, "AuditTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "AuditInfo", DbType.String);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoReportAudit entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "RaId", DbType.AnsiString, entity.RaId);
            db.AddInParameter(dbCommand, "ReportId", DbType.AnsiString, entity.ReportId);
            db.AddInParameter(dbCommand, "AuditorId", DbType.AnsiString, entity.AuditorId);
            db.AddInParameter(dbCommand, "FlowCode", DbType.AnsiString, entity.FlowCode);
            db.AddInParameter(dbCommand, "BeginStatus", DbType.Int32, entity.BeginStatus);
            db.AddInParameter(dbCommand, "EndStatus", DbType.Int32, entity.EndStatus);
            db.AddInParameter(dbCommand, "AuditTime", DbType.DateTime, entity.AuditTime);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, entity.CreateTime);
            db.AddInParameter(dbCommand, "AuditInfo", DbType.String, entity.AuditInfo);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoReportAudit entity)
        {
            db.SetParameterValue(addCmd, "RaId", entity.RaId);
            db.SetParameterValue(addCmd, "ReportId", entity.ReportId);
            db.SetParameterValue(addCmd, "AuditorId", entity.AuditorId);
            db.SetParameterValue(addCmd, "FlowCode", entity.FlowCode);
            db.SetParameterValue(addCmd, "BeginStatus", entity.BeginStatus);
            db.SetParameterValue(addCmd, "EndStatus", entity.EndStatus);
            db.SetParameterValue(addCmd, "AuditTime", entity.AuditTime);
            db.SetParameterValue(addCmd, "CreateTime", entity.CreateTime);
            db.SetParameterValue(addCmd, "AuditInfo", entity.AuditInfo);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "RaId", row["RaId"]);
            db.SetParameterValue(addCmd, "ReportId", row["ReportId"]);
            db.SetParameterValue(addCmd, "AuditorId", row["AuditorId"]);
            db.SetParameterValue(addCmd, "FlowCode", row["FlowCode"]);
            db.SetParameterValue(addCmd, "BeginStatus", row["BeginStatus"]);
            db.SetParameterValue(addCmd, "EndStatus", row["EndStatus"]);
            db.SetParameterValue(addCmd, "AuditTime", row["AuditTime"]);
            db.SetParameterValue(addCmd, "CreateTime", row["CreateTime"]);
            db.SetParameterValue(addCmd, "AuditInfo", row["AuditInfo"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "ReportId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "AuditorId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "FlowCode", DbType.AnsiString);
            db.AddInParameter(dbCommand, "BeginStatus", DbType.Int32);
            db.AddInParameter(dbCommand, "EndStatus", DbType.Int32);
            db.AddInParameter(dbCommand, "AuditTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "AuditInfo", DbType.String);
            db.AddInParameter(dbCommand, "RaId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoReportAudit entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "ReportId", DbType.AnsiString, entity.ReportId);
            db.AddInParameter(dbCommand, "AuditorId", DbType.AnsiString, entity.AuditorId);
            db.AddInParameter(dbCommand, "FlowCode", DbType.AnsiString, entity.FlowCode);
            db.AddInParameter(dbCommand, "BeginStatus", DbType.Int32, entity.BeginStatus);
            db.AddInParameter(dbCommand, "EndStatus", DbType.Int32, entity.EndStatus);
            db.AddInParameter(dbCommand, "AuditTime", DbType.DateTime, entity.AuditTime);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, entity.CreateTime);
            db.AddInParameter(dbCommand, "AuditInfo", DbType.String, entity.AuditInfo);
            db.AddInParameter(dbCommand, "RaId", DbType.AnsiString, entity.RaId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoReportAudit entity)
        {
            db.SetParameterValue(updateCmd, "ReportId", entity.ReportId);
            db.SetParameterValue(updateCmd, "AuditorId", entity.AuditorId);
            db.SetParameterValue(updateCmd, "FlowCode", entity.FlowCode);
            db.SetParameterValue(updateCmd, "BeginStatus", entity.BeginStatus);
            db.SetParameterValue(updateCmd, "EndStatus", entity.EndStatus);
            db.SetParameterValue(updateCmd, "AuditTime", entity.AuditTime);
            db.SetParameterValue(updateCmd, "CreateTime", entity.CreateTime);
            db.SetParameterValue(updateCmd, "AuditInfo", entity.AuditInfo);
            db.SetParameterValue(updateCmd, "RaId", entity.RaId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "ReportId", row["ReportId"]);
            db.SetParameterValue(updateCmd, "AuditorId", row["AuditorId"]);
            db.SetParameterValue(updateCmd, "FlowCode", row["FlowCode"]);
            db.SetParameterValue(updateCmd, "BeginStatus", row["BeginStatus"]);
            db.SetParameterValue(updateCmd, "EndStatus", row["EndStatus"]);
            db.SetParameterValue(updateCmd, "AuditTime", row["AuditTime"]);
            db.SetParameterValue(updateCmd, "CreateTime", row["CreateTime"]);
            db.SetParameterValue(updateCmd, "AuditInfo", row["AuditInfo"]);
            db.SetParameterValue(updateCmd, "RaId", row["RaId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "RaId", DbType.AnsiString, "RA_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "ReportId", DbType.AnsiString, "REPORT_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "AuditorId", DbType.AnsiString, "AUDITOR_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "FlowCode", DbType.AnsiString, "FLOW_CODE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "BeginStatus", DbType.Int32, "BEGIN_STATUS", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "EndStatus", DbType.Int32, "END_STATUS", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "AuditTime", DbType.DateTime, "AUDIT_TIME", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "CreateTime", DbType.DateTime, "CREATE_TIME", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "AuditInfo", DbType.String, "AUDIT_INFO", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            db.AddInParameter(dbUpdateCommand, "ReportId", DbType.AnsiString, "REPORT_ID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "AuditorId", DbType.AnsiString, "AUDITOR_ID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "FlowCode", DbType.AnsiString, "FLOW_CODE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "BeginStatus", DbType.Int32, "BEGIN_STATUS", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "EndStatus", DbType.Int32, "END_STATUS", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "AuditTime", DbType.DateTime, "AUDIT_TIME", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "CreateTime", DbType.DateTime, "CREATE_TIME", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "AuditInfo", DbType.String, "AUDIT_INFO", DataRowVersion.Current);
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "RaId", DbType.AnsiString, "RA_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "RaId", DbType.AnsiString, "RA_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "RaId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoReportAudit entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "RaId", DbType.AnsiString, entity.RaId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoReportAudit entity)
        {
            db.SetParameterValue(deleteCmd, "RaId", entity.RaId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "RaId", row["RaId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoReportAudit entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "RaId", DbType.AnsiString, entity.RaId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoReportAudit entity)
        {
            if (!reader.IsDBNull(0)) entity.RaId = reader.GetString(0);
            if (!reader.IsDBNull(1)) entity.ReportId = reader.GetString(1);
            if (!reader.IsDBNull(2)) entity.AuditorId = reader.GetString(2);
            if (!reader.IsDBNull(3)) entity.FlowCode = reader.GetString(3);
            if (!reader.IsDBNull(4)) entity.BeginStatus = reader.GetInt32(4);
            if (!reader.IsDBNull(5)) entity.EndStatus = reader.GetInt32(5);
            if (!reader.IsDBNull(6)) entity.AuditTime = reader.GetDateTime(6);
            if (!reader.IsDBNull(7)) entity.CreateTime = reader.GetDateTime(7);
            if (!reader.IsDBNull(8)) entity.AuditInfo = reader.GetString(8);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoReportAudit entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "RaId", DbType.AnsiString, entity.RaId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

